package com.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.FieldsDao;
import com.dao.IssueDao;
import com.dao.StatusDao;
import com.dao.UserDao;
import com.dto.UserDTO;
import com.entity.FieldsEntity;
import com.entity.IssueEntity;
import com.entity.UserEntity;
import com.service.status.StatusCode;

@Service
public class UserService {

	@Autowired
	UserDao userDao;
	@Autowired
	private StatusDao statusDao;
	@Autowired
	private FieldsDao fieldsDao;
	@Autowired
	private IssueDao issueDao;

	public void setUserDao(UserDao user) {
		this.userDao = user;
	}

	@Transactional
	public List<IssueEntity> getIssuesByDevelopersEmailAndStatus(String email,
			StatusCode... codes) {

		List<IssueEntity> issueList = new ArrayList<IssueEntity>();
		List<FieldsEntity> fieldsList = fieldsDao
				.getFieldsByDeveloperEmail(email);

		for (FieldsEntity field : fieldsList) {
			IssueEntity issue = issueDao
					.getIssueByFieldsId(field.getIdFields());
			System.out.println(field.getIdFields());
			issue = issueDao.getEagerIssueByUniqueId(issue.getId());
			System.out.println("Inside getIssuesByStatus service " + issue);
			
			for (StatusCode code : codes) {
				if (field.getStatusEntity().getIdStatus() == code.getId()) {
					issueList.add(issue);
					break;
				}
			}
			
		}
		return issueList;
	}

	@Transactional
	public List<IssueEntity> getIssuesByDevelopersEmail(String email) {

		List<IssueEntity> issueList = new ArrayList<IssueEntity>();
		List<FieldsEntity> fieldsList = fieldsDao
				.getFieldsByDeveloperEmail(email);

		for (FieldsEntity field : fieldsList) {
			IssueEntity issue = issueDao
					.getIssueByFieldsId(field.getIdFields());
			System.out.println(field.getIdFields());
			issue = issueDao.getEagerIssueByUniqueId(issue.getId());
			System.out.println("Inside getIssuesByStatus service " + issue);
			issueList.add(issue);
		}
		return issueList;
	}

	@Transactional
	public List<UserDTO> getAllUsers() {
		return mapUserDtoToUserEntity(userDao.getAllUser());
	}
	
	@Transactional
	public UserDTO getUserByEmail(String email){
		UserDTO user = new UserDTO();
		UserEntity entity = userDao.getUserByEmail(email);
		user.setName(entity.getName());
		user.setDisplayName(entity.getDisplayName());
		user.setActive(entity.getActive());
		user.setEmailAddress(entity.getEmailAddress());
		user.setSelf(entity.getSelf());
		return user;
	}
	


	private List<UserDTO> mapUserDtoToUserEntity(List<UserEntity> userEntities) {

		List<UserDTO> userDtoList = new ArrayList<UserDTO>();

		for (UserEntity userEntity : userEntities) {
			UserDTO userDto = new UserDTO();
			userDto.setEmailAddress(userEntity.getEmailAddress());
			userDtoList.add(userDto);
		}

		return userDtoList;
	}
}
